﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	public partial class HYDrawYXDrpgForm : System.Windows.Forms.Form
	{
		public bool BeLinkSelect { get; set; }

		public HYDrawYXDrpgForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public HYDrawYXDrpgForm(List<GraphicsStyle> gs, Dictionary<string, string> dicPara = null)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.LineStyle = gs;
			this.dic = dicPara;
		}

		private void HYDrawYXDrpgForm_Load(object sender, EventArgs e)
		{
			this.ReadUserData();
			this.InitUI();
		}

		private void InitCmb()
		{
			this.cmbMaterial.DataSource = this._strMaterial.ToList<string>();
			List<string> list = new List<string>();
			foreach (GraphicsStyle graphicsStyle in this.LineStyle)
			{
				if (!list.Contains(graphicsStyle.Name))
				{
					list.Add(graphicsStyle.Name);
				}
			}
			this.cmbLineIn.DataSource = list;
			List<string> list2 = new List<string>();
			foreach (GraphicsStyle graphicsStyle2 in this.LineStyle)
			{
				if (!list2.Contains(graphicsStyle2.Name))
				{
					list2.Add(graphicsStyle2.Name);
				}
			}
			if (!this.bLayInRoom)
			{
				this.ckbLinkSelect.Enabled = false;
			}
			else
			{
				this.ckbLinkSelect.Enabled = true;
			}
			this.cmbLineOut.DataSource = list2;
		}

		private GraphicsStyle GetLineStyleByNames(string name)
		{
			GraphicsStyle result = null;
			foreach (GraphicsStyle graphicsStyle in this.LineStyle)
			{
				if (name == graphicsStyle.Name)
				{
					result = graphicsStyle;
					break;
				}
			}
			return result;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.hytbDis.Text.Trim() == "" || this.hytbDisToWall.Text.Trim() == "" || this.hytbPipeDN.Text.Trim() == "" || this.hytbRadius.Text.Trim() == "")
			{
				YJKMessageBox.Information("参数不能有空！");
				return;
			}
			this.bLayInRoom = this.chbLayRoom.Checked;
			if (this.bLayInRoom)
			{
				this.BeLinkSelect = this.ckbLinkSelect.Checked;
			}
			else
			{
				this.BeLinkSelect = false;
			}
			this.bMarkTag = this.chbMark.Checked;
			this.ReadUIData();
			this.SaveData();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnLineTypeEditIN_Click(object sender, EventArgs e)
		{
			GraphicsStyle lineStyleByNames = this.GetLineStyleByNames(this.cmbLineIn.Text);
			new LineStyleEditForm(this.cmbLineIn.Text, lineStyleByNames).ShowDialog();
		}

		private void btnLineTypeEditOut_Click(object sender, EventArgs e)
		{
			GraphicsStyle lineStyleByNames = this.GetLineStyleByNames(this.cmbLineOut.Text);
			new LineStyleEditForm(this.cmbLineOut.Text, lineStyleByNames).ShowDialog();
		}

		private void InitUI()
		{
			this.InitCmb();
			this.hytbDis.Text = this.drawYxFloorPipe.Dis.ToString();
			this.hytbDisToWall.Text = this.drawYxFloorPipe.DisToWall.ToString();
			this.hytbRadius.Text = this.drawYxFloorPipe.DisRadius.ToString();
			this.hytbPipeMaterial.Text = this.drawYxFloorPipe.PipeMaterial;
			this.cmbMaterial.SelectedItem = this.drawYxFloorPipe.PipeMaterial;
			this.hytbPipeDN.Text = this.drawYxFloorPipe.PipeDN.ToString();
			if (this.drawYxFloorPipe.IsDouble == 1)
			{
				this.rbDouble.Checked = true;
				this.rbSingle.Checked = false;
			}
			else
			{
				this.rbDouble.Checked = false;
				this.rbSingle.Checked = true;
			}
			if (this.drawYxFloorPipe.InType != null)
			{
				this.cmbLineIn.SelectedItem = this.drawYxFloorPipe.InType.Name;
			}
			else
			{
				GraphicsStyle graphicsStyle = this.LineStyle.FirstOrDefault((GraphicsStyle s) => s.Name == "地热盘管供水");
				if (graphicsStyle == null)
				{
					this.cmbLineIn.SelectedIndex = 0;
					this.drawYxFloorPipe.InType = this.LineStyle[0];
				}
				else
				{
					this.cmbLineIn.SelectedItem = graphicsStyle.Name;
					this.drawYxFloorPipe.InType = graphicsStyle;
				}
			}
			if (this.drawYxFloorPipe.OutType != null)
			{
				this.cmbLineOut.SelectedItem = this.drawYxFloorPipe.OutType.Name;
			}
			else
			{
				GraphicsStyle graphicsStyle2 = this.LineStyle.FirstOrDefault((GraphicsStyle s) => s.Name == "地热盘管回水");
				if (graphicsStyle2 == null)
				{
					this.cmbLineOut.SelectedIndex = 1;
					this.drawYxFloorPipe.OutType = this.LineStyle[1];
				}
				else
				{
					this.cmbLineOut.SelectedItem = graphicsStyle2.Name;
					this.drawYxFloorPipe.OutType = graphicsStyle2;
				}
			}
			this.chbLayRoom.Checked = this.bLayInRoom;
			this.chbMark.Checked = this.bMarkTag;
			this.ckbLinkSelect.Checked = this.BeLinkSelect;
			if (this.dic != null)
			{
				double num = double.Parse(this.dic["distance"]);
				string selectedItem = this.dic["Material"];
				this.hytbDis.Text = num.ToString();
				this.cmbMaterial.SelectedItem = selectedItem;
				this.chbLayRoom.Checked = true;
			}
		}

		private void ReadUIData()
		{
			this.drawYxFloorPipe.IsDouble = (this.rbDouble.Checked ? 1 : 0);
			this.drawYxFloorPipe.Dis = double.Parse(this.hytbDis.Text) / 304.8;
			this.drawYxFloorPipe.DisToWall = double.Parse(this.hytbDisToWall.Text) / 304.8;
			this.drawYxFloorPipe.DisRadius = double.Parse(this.hytbRadius.Text) / 304.8;
			this.drawYxFloorPipe.PipeDN = double.Parse(this.hytbPipeDN.Text) / 304.8;
			this.drawYxFloorPipe.PipeMaterial = this.cmbMaterial.Text;
		}

		private void SaveData()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (PropertyInfo propertyInfo in this.drawYxFloorPipe.GetType().GetProperties())
			{
				if (propertyInfo.Name == "InType" || propertyInfo.Name == "OutType")
				{
					dictionary.Add(propertyInfo.Name, (propertyInfo.GetValue(this.drawYxFloorPipe) as GraphicsStyle).Name.ToString());
				}
				else
				{
					dictionary.Add(propertyInfo.Name, propertyInfo.GetValue(this.drawYxFloorPipe).ToString());
				}
			}
			dictionary.Add("bLayInRoom", this.bLayInRoom.ToString());
			dictionary.Add("MarkTag", this.bMarkTag.ToString());
			dictionary.Add("BeLinkSelect", this.ckbLinkSelect.Checked.ToString());
			this.userSetting.WriteYxFloorPipeDrawSetting(dictionary);
			string strLineTypeName = this.rbSingle.Checked ? "FloorYxPipeDrawSingle" : "FloorYxPipeDrawDouble";
			this.userSetting.WriteYxFloorPipeDrawSetting(dictionary, strLineTypeName);
		}

		private void ReadUserData()
		{
			Dictionary<string, string> yxFloorPipeDrawSetting = this.userSetting.GetYxFloorPipeDrawSetting();
			foreach (PropertyInfo propertyInfo in this.drawYxFloorPipe.GetType().GetProperties())
			{
				if (!(yxFloorPipeDrawSetting[propertyInfo.Name] == ""))
				{
					if (propertyInfo.Name == "InType" || propertyInfo.Name == "OutType")
					{
						propertyInfo.SetValue(this.drawYxFloorPipe, this.GetLineStyleByNames(yxFloorPipeDrawSetting[propertyInfo.Name]));
					}
					else if (propertyInfo.Name == "IsDouble")
					{
						propertyInfo.SetValue(this.drawYxFloorPipe, int.Parse(yxFloorPipeDrawSetting[propertyInfo.Name]));
					}
					else if (propertyInfo.Name == "PipeMaterial")
					{
						propertyInfo.SetValue(this.drawYxFloorPipe, yxFloorPipeDrawSetting[propertyInfo.Name]);
					}
					else
					{
						propertyInfo.SetValue(this.drawYxFloorPipe, Math.Round(double.Parse(yxFloorPipeDrawSetting[propertyInfo.Name]) * 304.8, 0));
					}
				}
			}
			this.bLayInRoom = (yxFloorPipeDrawSetting["bLayInRoom"] != "" && bool.Parse(yxFloorPipeDrawSetting["bLayInRoom"]));
			this.bMarkTag = (yxFloorPipeDrawSetting["MarkTag"] != "" && bool.Parse(yxFloorPipeDrawSetting["MarkTag"]));
			this.BeLinkSelect = (yxFloorPipeDrawSetting["BeLinkSelect"] != "" && bool.Parse(yxFloorPipeDrawSetting["BeLinkSelect"]));
		}

		private void btnSetting_Click(object sender, EventArgs e)
		{
			LineStyleEditForm lineStyleEditForm;
			if (this.rbSingle.Checked)
			{
				lineStyleEditForm = new LineStyleEditForm(this.drawYxFloorPipe.InType, this.drawYxFloorPipe.OutType, this.LineStyle, true);
			}
			else
			{
				lineStyleEditForm = new LineStyleEditForm(this.drawYxFloorPipe.InType, this.drawYxFloorPipe.OutType, this.LineStyle, false);
			}
			if (lineStyleEditForm.ShowDialog() == DialogResult.OK)
			{
				this.drawYxFloorPipe.InType = lineStyleEditForm.graphicStyleIn;
				this.drawYxFloorPipe.OutType = lineStyleEditForm.graphicStyleOut;
			}
		}

		private void hytbRadius_Validating(object sender, CancelEventArgs e)
		{
			if (this.hytbRadius.Text.Trim() == "")
			{
				YJKMessageBox.Information("转角半径不能为空！");
				this.hytbRadius.Undo();
				this.hytbRadius.Focus();
				return;
			}
			double num = double.Parse(this.hytbPipeDN.Text);
			double num2 = double.Parse(this.hytbRadius.Text);
			if ((num2 < 8.0 * num || num2 > 11.0 * num) && DialogResult.No == YJKMessageBox.Show("根据《辐射供暖供冷技术规程》JGJ 142-2012中5.4.3的规定，塑\n料管弯曲半径不应小于管道外径的8倍，最大弯曲半径不得大于\n管道外径的11倍。是否继续使用当前转角半径数值？", MessageBoxButtons.YesNo))
			{
				this.hytbRadius.Undo();
				this.hytbRadius.Focus();
			}
		}

		private void hytbPipeDN_Validating(object sender, CancelEventArgs e)
		{
			if (this.hytbPipeDN.Text.Trim() == "")
			{
				YJKMessageBox.Information("管径信息不能为空！");
				this.hytbPipeDN.Undo();
				this.hytbPipeDN.Focus();
			}
		}

		private void chbLayRoom_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbLayRoom.Checked)
			{
				this.ckbLinkSelect.Enabled = true;
				return;
			}
			this.ckbLinkSelect.Enabled = false;
		}

		private void rbSingle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbSingle.Checked)
			{
				this.drawYxFloorPipe.InType = this.LineStyle[0];
				this.drawYxFloorPipe.OutType = this.LineStyle[0];
			}
			else if (this.rbDouble.Checked)
			{
				this.drawYxFloorPipe.InType = this.LineStyle[0];
				this.drawYxFloorPipe.OutType = this.LineStyle[1];
			}
			string strLineTypeName = this.rbSingle.Checked ? "FloorYxPipeDrawSingle" : "FloorYxPipeDrawDouble";
			Dictionary<string, string> yxFloorPipeDrawSetting = this.userSetting.GetYxFloorPipeDrawSetting(strLineTypeName);
			foreach (PropertyInfo propertyInfo in this.drawYxFloorPipe.GetType().GetProperties())
			{
				if (!(yxFloorPipeDrawSetting[propertyInfo.Name] == "") && (propertyInfo.Name == "InType" || propertyInfo.Name == "OutType"))
				{
					propertyInfo.SetValue(this.drawYxFloorPipe, this.GetLineStyleByNames(yxFloorPipeDrawSetting[propertyInfo.Name]));
				}
			}
		}

		public DrawYxFloorPipeData drawYxFloorPipe = new DrawYxFloorPipeData();

		public bool bLayInRoom;

		public bool bMarkTag;

		private List<GraphicsStyle> LineStyle;

		private string[] _strMaterial = new string[]
		{
			"PPR",
			"PE-X",
			"PB",
			"PE-RT"
		};

		private Dictionary<string, string> dic;

		private HVACUserSetting userSetting = new HVACUserSetting();
	}
}
